package com.injurydetection.www.view;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.injurydetection.www.MyApplication;
import com.injurydetection.www.R;
import com.injurydetection.www.adapter.ErjianAdapter;
import com.injurydetection.www.bean.DataBean;
import com.injurydetection.www.bean.ErjianRv;
import com.injurydetection.www.bean.MesEventCount;
import com.injurydetection.www.greendao.DataBeanDao;
import com.injurydetection.www.widget.DeleteDialog;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class CountDownFragment extends Fragment {
    int x = R.layout.fragment_count_down;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R.id.deleteall_tv)
    TextView deleteallTv;

    private Unbinder mUnbinder;
    private LinearLayoutManager mLinearLayoutManager;

    public CountDownFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_count_down, container, false);
        mUnbinder = ButterKnife.bind(this, view);
        initData();
        initRv();

        deleteallTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mList.size() == 0) {
                    return;
                }
                DeleteDialog deleteDialog = new DeleteDialog(getContext(), R.style.TipDialog);
                deleteDialog.setCommitListener(new DeleteDialog.CommitListener() {
                    @Override
                    public void commit() {
                        //获得最新的list
                        for (int i = 0; i < mList.size(); i++) {
                            DataBean dataBeanOld = mList.get(i);
                            dataBeanOld.setIsChecked(false);

                            MyApplication.getDaoSession().getDataBeanDao().update(dataBeanOld);
                        }
                        mList.clear();
                        ErjianAdapter adapter = new ErjianAdapter(mList, getContext());
                        rv.setAdapter(adapter);
                        EventBus.getDefault().post(new MesEventCount(0));

//                        MyApplication.getDaoSession().getDataBeanDao().deleteAll();
//                        mList.clear();
//                        ErjianAdapter adapter = new ErjianAdapter(mList, getContext());
//                        rv.setAdapter(adapter);
                    }
                });
                deleteDialog.show();
                deleteDialog.getWindow().setLayout(1000, 500);
            }
        });
        // Inflate the layout for this fragment
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }

    private List<DataBean> mList;

    /**
     * 初始化rv数据
     */
    private void initData() {
        mList = new ArrayList<>();
        List<DataBean> listTemp = MyApplication.getDaoSession().getDataBeanDao().queryBuilder()
                .where(DataBeanDao.Properties.IsChecked.eq(true))
                .orderAsc(DataBeanDao.Properties.CreateTime)
                .build().list();

        if (listTemp.size() > 0) {
            for (int i1 = 0; i1 < listTemp.size(); i1++) {
                if ((new Date().getTime() - listTemp.get(i1).getCreateTime().getTime()) / 1000 / 60 / 60 <= 5) {
                    mList.add(listTemp.get(i1));
                }
            }
        }
    }

    private void initRv() {
        mLinearLayoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false);
        rv.setLayoutManager(mLinearLayoutManager);
        ErjianAdapter erjianAdapter = new ErjianAdapter(mList, getContext());
        rv.setAdapter(erjianAdapter);
    }

}
